package com.camelot.boot.server;

import org.apache.poi.xwpf.usermodel.*;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;


public class CreateWordDocument {



            public static void main(String[] args) throws Exception {
                // 创建新文档
                XWPFDocument document = new XWPFDocument();

                // 创建表格（初始1行16列）
                XWPFTable table = document.createTable(1, 16);

                // 设置表格属性（带边框）
                setupBorderedTableProperties(table);

                // 构建表格内容
                buildTableContent(table);

                // 添加底部声明
                addFooter(document);

                // 保存文档
                saveDocument(document, "人员简历_带边框模板.docx");

                System.out.println("带边框的简历模板生成完成！");
            }

    private static void setupBorderedTableProperties(XWPFTable table) {
        // 设置表格宽度
        CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9000));

        // 设置表格边框（单线边框）
        CTTblBorders borders = table.getCTTbl().getTblPr().addNewTblBorders();
        borders.addNewBottom().setVal(STBorder.SINGLE);
        borders.addNewLeft().setVal(STBorder.SINGLE);
        borders.addNewRight().setVal(STBorder.SINGLE);
        borders.addNewTop().setVal(STBorder.SINGLE);
        borders.addNewInsideH().setVal(STBorder.SINGLE);
        borders.addNewInsideV().setVal(STBorder.SINGLE);

        // 设置列宽
        CTTblGrid grid = table.getCTTbl().addNewTblGrid();
        int[] columnWidths = {600, 100, 100, 600, 300, 100, 600, 100, 450,
                100, 250, 150, 300, 100, 450, 550};
        for (int w : columnWidths) {
            grid.addNewGridCol().setW(BigInteger.valueOf(w));
        }
    }

    private static void buildTableContent(XWPFTable table) {
        // 基本信息行
        createInfoRows(table);

        // 教育培训经历
        createEducationSection(table);

        // 工作经历
        createWorkExperienceSection(table);

        // 项目经验
        createProjectSection(table);
    }

    private static void createInfoRows(XWPFTable table) {
        // 第1行：姓名、性别、出生年月
        XWPFTableRow row1 = table.getRow(0);
        setCellText(row1, 0, "姓 名", true);
        mergeCells(row1, 1, 3, "");
        setCellText(row1, 4, "性 别", true);
        mergeCells(row1, 5, 6, "");
        mergeCells(row1, 7, 8, "");
        setCellText(row1, 9, "出 生\n年 月", true);
        mergeCells(row1, 10, 12, "");
        mergeCells(row1, 13, 15, "");

        // 第2行：民族、籍贯、政治面貌
        XWPFTableRow row2 = table.createRow();
        setCellText(row2, 0, "民 族", true);
        mergeCells(row2, 1, 3, "");
        setCellText(row2, 4, "籍 贯", true);
        mergeCells(row2, 5, 6, "");
        mergeCells(row2, 7, 8, "");
        setCellText(row2, 9, "政 治\n面 貌", true);
        mergeCells(row2, 10, 12, "");
        mergeCells(row2, 13, 15, "");

        // 第3行：婚姻状况、职称、工作年限
        XWPFTableRow row3 = table.createRow();
        setCellText(row3, 0, "婚姻状况", true);
        mergeCells(row3, 1, 3, "");
        setCellText(row3, 4, "职 称", true);
        mergeCells(row3, 5, 6, "");
        mergeCells(row3, 7, 8, "");
        setCellText(row3, 9, "工作年限", true);
        mergeCells(row3, 10, 12, "");
        mergeCells(row3, 13, 15, "");

        // 第4行：常驻地址、联系电话
        XWPFTableRow row4 = table.createRow();
        setCellText(row4, 0, "常驻地址", true);
        mergeCells(row4, 1, 7, "");
        setCellText(row4, 8, "联系电话", true);
        mergeCells(row4, 9, 15, "");

        // 第5行：身份证号、业务方向
        XWPFTableRow row5 = table.createRow();
        setCellText(row5, 0, "身份证号", true);
        mergeCells(row5, 1, 7, "");
        setCellText(row5, 8, "业务方向", true);
        mergeCells(row5, 9, 15, "");
    }

    private static void createEducationSection(XWPFTable table) {
        // 标题行
        XWPFTableRow titleRow = table.createRow();
        mergeCells(titleRow, 0, 15, "教育培训经历", true);

        // 表头行
        XWPFTableRow headerRow = table.createRow();
        mergeCells(headerRow, 0, 1, "开始时间", true);
        mergeCells(headerRow, 2, 3, "结束时间", true);
        mergeCells(headerRow, 4, 7, "机构名称", true);
        setCellText(headerRow, 8, "专业", true);
        mergeCells(headerRow, 9, 11, "学历", true);
        mergeCells(headerRow, 12, 13, "形式", true);
        mergeCells(headerRow, 14, 15, "备注", true);

        // 内容行
        XWPFTableRow contentRow = table.createRow();
        mergeCells(contentRow, 0, 1, "");
        mergeCells(contentRow, 2, 3, "");
        mergeCells(contentRow, 4, 7, "");
        setCellText(contentRow, 8, "");
        mergeCells(contentRow, 9, 11, "");
        mergeCells(contentRow, 12, 13, "");
        setCellText(contentRow, 14, "◻ 就业学历\n◻ 最高学历\n◻ 职业资格");
    }

    private static void createWorkExperienceSection(XWPFTable table) {
        // 标题行
        XWPFTableRow titleRow = table.createRow();
        mergeCells(titleRow, 0, 15, "工作经历", true);

        // 表头行
        XWPFTableRow headerRow = table.createRow();
        mergeCells(headerRow, 0, 1, "开始时间", true);
        mergeCells(headerRow, 2, 3, "结束时间", true);
        mergeCells(headerRow, 4, 8, "公司全称", true);
        mergeCells(headerRow, 9, 13, "部门", true);
        mergeCells(headerRow, 14, 15, "岗位/职位", true);

        // 3行内容
        for (int i = 0; i < 3; i++) {
            XWPFTableRow row = table.createRow();
            mergeCells(row, 0, 1, "");
            mergeCells(row, 2, 3, "");
            mergeCells(row, 4, 8, "");
            mergeCells(row, 9, 13, "");
            mergeCells(row, 14, 15, "");
        }
    }

    private static void createProjectSection(XWPFTable table) {
        // 标题行
        XWPFTableRow titleRow = table.createRow();
        mergeCells(titleRow, 0, 15, "项目经验", true);

        // 项目1
        createProjectRow(table, "1.项目名称");

        // 项目2
        createProjectRow(table, "2.项目名称");
    }

    private static void createProjectRow(XWPFTable table, String projectName) {
        // 项目名称行
        XWPFTableRow nameRow = table.createRow();
        mergeCells(nameRow, 0, 2, projectName, true);
        mergeCells(nameRow, 3, 4, "");
        mergeCells(nameRow, 5, 6, "进入项目时间", true);
        mergeCells(nameRow, 7, 10, "");
        mergeCells(nameRow, 11, 14, "退出项目时间", true);
        setCellText(nameRow, 15, "");

        // 项目描述行
        XWPFTableRow descRow = table.createRow();
        mergeCells(descRow, 0, 2, "项目描述", true);
        mergeCells(descRow, 3, 4, "");
        mergeCells(descRow, 5, 6, "工作职责", true);
        mergeCells(descRow, 7, 15, "");
    }

    private static void addFooter(XWPFDocument document) {
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("我公司已核实该应聘者简历上的个人信息，确保各项信息真实、准确、有效。");
        paragraph.setAlignment(ParagraphAlignment.LEFT);
    }

    private static void saveDocument(XWPFDocument document, String filename) throws Exception {
        try (FileOutputStream out = new FileOutputStream(filename)) {
            document.write(out);
        }
        document.close();
    }

    private static void setCellText(XWPFTableRow row, int col, String text) {
        setCellText(row, col, text, false);
    }

    private static void setCellText(XWPFTableRow row, int col, String text, boolean bold) {
        XWPFTableCell cell = getOrCreateCell(row, col);
        cell.removeParagraph(0);
        XWPFParagraph paragraph = cell.addParagraph();
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun run = paragraph.createRun();
        run.setText(text);
        run.setBold(bold);

        // 设置单元格边框
        setCellBorders(cell);
    }

    private static void mergeCells(XWPFTableRow row, int startCol, int endCol, String text) {
        mergeCells(row, startCol, endCol, text, false);
    }

    private static void mergeCells(XWPFTableRow row, int startCol, int endCol, String text, boolean bold) {
        XWPFTableCell startCell = getOrCreateCell(row, startCol);

        // 设置合并范围
        CTTcPr tcPr = startCell.getCTTc().isSetTcPr() ? startCell.getCTTc().getTcPr() : startCell.getCTTc().addNewTcPr();
        tcPr.addNewGridSpan().setVal(BigInteger.valueOf(endCol - startCol + 1));

        // 设置文本
        setCellText(row, startCol, text, bold);

        // 设置被合并单元格的边框
        for (int i = startCol + 1; i <= endCol; i++) {
            XWPFTableCell cell = getOrCreateCell(row, i);
            cell.removeParagraph(0);
            cell.addParagraph();
            setCellBorders(cell);
        }
    }

    private static XWPFTableCell getOrCreateCell(XWPFTableRow row, int col) {
        while (row.getTableCells().size() <= col) {
            row.createCell();
        }
        return row.getCell(col);
    }

    private static void setCellBorders(XWPFTableCell cell) {
        CTTcPr tcPr = cell.getCTTc().isSetTcPr() ? cell.getCTTc().getTcPr() : cell.getCTTc().addNewTcPr();
        CTTcBorders borders = tcPr.isSetTcBorders() ? tcPr.getTcBorders() : tcPr.addNewTcBorders();
        borders.addNewBottom().setVal(STBorder.SINGLE);
        borders.addNewLeft().setVal(STBorder.SINGLE);
        borders.addNewRight().setVal(STBorder.SINGLE);
        borders.addNewTop().setVal(STBorder.SINGLE);
    }
}